
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cmd_obj_ext.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "organizationao_adaptor.h"
#include "organizationao_req_resp.h"


std::string g_sorganizationaoStandAlone;

///////////////////////
// class COrganizationAoSvcFactory
int COrganizationAoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
    pCntl->getConfig("StandAlone", g_sorganizationaoStandAlone);
         

	return COrganizationAo::Initialize(pCntl);
}

int COrganizationAoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int COrganizationAoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t COrganizationAoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* COrganizationAoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new COrganizationAoService(dwcmdId));
}

void COrganizationAoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[ORGANIZATIONAO_DELETEBRANCH_REQ_CMD_ID] = true;
	mapSvcType[ORGANIZATIONAO_FINDBRANCH_REQ_CMD_ID] = true;
	mapSvcType[ORGANIZATIONAO_GETBRANCH_REQ_CMD_ID] = true;
	mapSvcType[ORGANIZATIONAO_MODIFYBRANCH_REQ_CMD_ID] = true;
}

////////////////////////
// class COrganizationAoService
COrganizationAoService::COrganizationAoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    enum DeployMethod method = DEPLOY_DETACH;
    if(!g_sorganizationaoStandAlone.empty() && g_sorganizationaoStandAlone != "0") // Stand-Alone
        method = DEPLOY_STANDALONE;
        
    m_pSvc = new COrganizationAo(method);
}

void COrganizationAoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void COrganizationAoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int COrganizationAoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case ORGANIZATIONAO_DELETEBRANCH_REQ_CMD_ID:
        return DeleteBranch(oCmd);
	case ORGANIZATIONAO_FINDBRANCH_REQ_CMD_ID:
        return FindBranch(oCmd);
	case ORGANIZATIONAO_GETBRANCH_REQ_CMD_ID:
        return GetBranch(oCmd);
	case ORGANIZATIONAO_MODIFYBRANCH_REQ_CMD_ID:
        return ModifyBranch(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int COrganizationAoService::DeleteBranch(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    COrganizationAoDeleteBranchReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	COrganizationAoDeleteBranchResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteBranch(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.option,
		oReq.branchId,
		oReq.inReserve,
		oReq.errmsg,
		oReq.errorCode,
		oReq.errorMsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.errorCode = oReq.errorCode;
	oResp.errorMsg = oReq.errorMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int COrganizationAoService::FindBranch(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    COrganizationAoFindBranchReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	COrganizationAoFindBranchResp oResp;
	
    oResp.dwResult = m_pSvc->FindBranch(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.option,
		oReq.filter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.total,
		oReq.branch,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.total = oReq.total;
	oResp.branch = oReq.branch;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int COrganizationAoService::GetBranch(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    COrganizationAoGetBranchReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	COrganizationAoGetBranchResp oResp;
	
    oResp.dwResult = m_pSvc->GetBranch(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.option,
		oReq.branchId,
		oReq.inReserve,
		oReq.errmsg,
		oReq.errorCode,
		oReq.errorMsg,
		oReq.branch,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.errorCode = oReq.errorCode;
	oResp.errorMsg = oReq.errorMsg;
	oResp.branch = oReq.branch;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int COrganizationAoService::ModifyBranch(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    COrganizationAoModifyBranchReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	COrganizationAoModifyBranchResp oResp;
	
    oResp.dwResult = m_pSvc->ModifyBranch(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.option,
		oReq.branch,
		oReq.inReserve,
		oReq.errmsg,
		oReq.branchBack,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.branchBack = oReq.branchBack;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
